package day14;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class TestMap {
    public static void main(String[] args) {
        Map<String, Integer> map = new HashMap<>();
        map.put("A", 1);
        map.put("B", 2);
        map.put("C", 3);
        map.put("D", 4);
        map.put("E", 5);
        map.put(null, null);
        map.put("A", 6);
        map.put("F", 2);
        System.out.println(map);
        Integer value = map.get("A");
        System.out.println(value);
        map.remove("A");
        System.out.println(map);
        System.out.println(map.keySet());
        //遍历
        //没有下标  不能使用标准for  遍历
        //迭代器：map不能直接使用  iterator()是Collection
        //方法  所以map只能通过间接方式做遍历
        System.out.println("迭代器");
        Iterator<String> iterator = map.keySet().iterator();
        while (iterator.hasNext()) {
            String key = iterator.next();
            Integer val = map.get(key);
            System.out.println(key + ":" + val);
        }

        System.out.println("增强for.....");
        for (String key : map.keySet()) {
            System.out.println(key + ":" + map.get(key));
        }
        System.out.println("借助于Entry对象做遍历");
        for(Map.Entry<String,Integer> o : map.entrySet()) {
            String key = o.getKey();
            Integer val = o.getValue();
            System.out.println(key + ":" + val);
        }
    }
}
